/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elementsDefense.gui;

import com.jme3.app.state.AbstractAppState;
import com.jme3.asset.AssetManager;
import com.jme3.input.controls.ActionListener;
import com.jme3.scene.Node;
import elementsDefense.EBus;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;

/**
 *
 * @author Ricardo
 */
public class HUD extends AbstractAppState implements PropertyChangeListener{
    
    int screenWidth,screenHeight;
    public static ActionListener actionListener;
    ArrayList<Button> buttons = new ArrayList<Button>();
    
    public PutTowerDialog towerDialog;
    
    private Label lblWave;
    private Label lblGold;
    private Label lblHitpoints;

    public HUD(AssetManager assetManager, Node guiNode, ActionListener actionListener, int screenWidth, int screenHeight) {
        HUD.actionListener=actionListener;
        EBus.addPropertyChangeListener(this);
        
        towerDialog=new PutTowerDialog(assetManager, guiNode, screenWidth, screenHeight);
        
        
        lblWave = new Label(screenWidth/2, screenHeight, screenWidth, screenHeight, assetManager, guiNode);
        lblGold = new Label(0, screenHeight, screenWidth, screenHeight, assetManager, guiNode);
        lblHitpoints = new Label(screenWidth-70, screenHeight, screenWidth, screenHeight, assetManager, guiNode);
        
//        int bSize = (int) (screenHeight*0.15);
//        
//        flowSpeed = new Label(0, screenHeight-bSize, screenWidth, screenHeight, assetManager, guiNode);
//        freezeCooldown = new Label(screenWidth-bSize, screenHeight-bSize, screenWidth, screenHeight, assetManager, guiNode);
//        level = new Label(screenWidth/2, screenHeight, screenWidth, screenHeight, assetManager, guiNode);
// 
//        
//        Button openflow = new Button(0, screenHeight, bSize, bSize, "openflow", assetManager, guiNode);
//        buttons.add(openflow);
//        
//        Button freeze = new Button(screenWidth-bSize, screenHeight, bSize, bSize, "freeze", assetManager, guiNode);
//        buttons.add(freeze);
    }

    @Override
    public void update(float tpf) {
//        flowSpeed.setText(Main.water.getFlowSpeedM3()+"");
//        if(Main.freezeCooldown==0){
//            freezeCooldown.setText("Ready");
//        }else{
//            freezeCooldown.setText((int)Main.freezeCooldown+"s");
//        }
//        level.setText("LVL "+Main.LEVEL);
    }
    
    
    
    public boolean testEvent(int x, int y, boolean isPressed){
        if(towerDialog.testEvent(x, y, isPressed)) return true;
        for (Button button : buttons) {
            if (button.contains(x, y)) {
                button.click();
                return true;
            }
        }
        return false;
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(EBus.Property.WAVE_NUMBER.toString())) {
            Integer wave=(Integer) evt.getNewValue();
            lblWave.setText("Wave "+wave);
        }else if (evt.getPropertyName().equals(EBus.Property.WAVE_COUNTDOWN.toString())) {
            Integer wave=(Integer) evt.getNewValue();
            lblWave.setText("Next Wave in "+wave);
        }else if (evt.getPropertyName().equals(EBus.Property.GOLD.toString())) {
            Integer gold=(Integer) evt.getNewValue();
            lblGold.setText("Gold: "+gold);
        }else if (evt.getPropertyName().equals(EBus.Property.HITPOINTS.toString())) {
            Integer hp=(Integer) evt.getNewValue();
            lblHitpoints.setText("HP: "+hp);
        }
    }
    
    
    
}
